package com.example.textgradient

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.example.textgradient.ColorTrackTextView.Direction


class MainActivity : AppCompatActivity() {

    private val TAG = "MainActivity"
    private val items = arrayOf("直播", "推荐", "视频", "图片", "段子", "精华")
    private var colorTrackTextView: ColorTrackTextView? = null
    private var viewPager2: ViewPager2? = null
    private var mIndicatorContainer: LinearLayout? = null
    private var mIndicators: MutableList<ColorTrackTextView>? = null
    private lateinit var frglist: ArrayList<BlankFragment>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        colorTrackTextView = findViewById<ColorTrackTextView>(R.id.mColorTrackTextView)
        viewPager2 = findViewById<ViewPager2>(R.id.view_pager2)
        mIndicators = mutableListOf()
        mIndicatorContainer = findViewById(R.id.indicator_view)
        if (!::frglist.isInitialized) {
            frglist = ArrayList<BlankFragment>()
            frglist.add(BlankFragment())
            frglist.add(BlankFragment())
            frglist.add(BlankFragment())
            frglist.add(BlankFragment())
            frglist.add(BlankFragment())
            frglist.add(BlankFragment())

        }

        initIndicator()
        initViewpager()
    }

    fun right(view: View) {
        setAnimation(Direction.RIGHT_TO_LEFT)

    }

    private fun initViewpager() {

        viewPager2?.adapter = ViewPagerAdapter(this, frglist)
        viewPager2?.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int, positionOffset: Float, positionOffsetPixels: Int
            ) {
                val left = mIndicators?.get(position)
                left?.setDirection(Direction.RIGHT_TO_LEFT)
                left?.setCurrentProgress(1 - positionOffset)

                try {
                    val right = mIndicators?.get(position + 1)
                    right?.setDirection(Direction.LEFT_TO_RIGHT)
                    right?.setCurrentProgress(positionOffset)
                } catch (e: Exception) {
                    Log.e(TAG,e.message.toString())
                }

            }
        })

    }

    private fun setAnimation(direction: Direction) {

        colorTrackTextView?.setDirection(direction)
        val a: ValueAnimator = ObjectAnimator.ofFloat(0F, 1F)
        a.apply {
            duration = 2000
            addUpdateListener {
                colorTrackTextView?.setCurrentProgress(it.animatedValue as Float)
            }
            start()
        }

    }

    fun left(view: View) {
        setAnimation(Direction.LEFT_TO_RIGHT)

    }

    fun initIndicator() {
        for (item in items) {
            val params = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
            )
            params.weight = 1F
            val colorTrackText = ColorTrackTextView(this)
            colorTrackText.apply {
                textSize = 20F
                setChangeColor(Color.RED)
                text = item
                layoutParams = params
            }
            mIndicatorContainer?.addView(colorTrackText)
            mIndicators?.add(colorTrackText)
        }
    }

}